package org.mall.modules.pay.service.impl;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.mall.common.constant.PayNotifyType;
import org.mall.common.constant.PayTransactionNotifyStatusEnum;
import org.mall.common.util.DateUtil;
import org.mall.common.util.json.JacksonUtils;
import org.mall.modules.pay.convert.PayNotifyConvert;
import org.mall.modules.pay.dataobject.PayNotifyTaskDO;
import org.mall.modules.pay.dataobject.PayRefundDO;
import org.mall.modules.pay.dataobject.PayTransactionDO;
import org.mall.modules.pay.dataobject.PayTransactionExtensionDO;
import org.mall.modules.pay.mapper.PayNotifyLogMapper;
import org.mall.modules.pay.mapper.PayNotifyTaskMapper;
import org.mall.modules.pojo.pay.message.PayRefundSuccessMessage;
import org.mall.modules.pojo.pay.message.PayTransactionSuccessMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class PayNotifyServiceImpl {

    //TODO  支付会掉
    @Autowired
    private PayNotifyLogMapper payTransactionNotifyLogMapper;
    @Autowired
    private PayNotifyTaskMapper payTransactionNotifyTaskMapper;
    @Autowired
    private DefaultMQProducer defaultMQProducer;
    @Deprecated // 参见 PayRefundSuccessConsumer 类的说明
    public void addRefundNotifyTask(PayRefundDO refund) {
        PayNotifyTaskDO payTransactionNotifyTask = this.createBasePayNotifyTaskDO(refund.getAppId(), refund.getNotifyUrl())
                .setType(PayNotifyType.REFUND.getValue());
        // 设置 Refund 属性
        payTransactionNotifyTask.setRefund(new PayNotifyTaskDO.Refund().setRefundId(refund.getId())
                .setTransactionId(refund.getTransactionId()).setOrderId(refund.getOrderId()));
        // 保存到数据库
        payTransactionNotifyTaskMapper.insert(payTransactionNotifyTask);
        // 发送 MQ 消息
        sendNotifyMessage(payTransactionNotifyTask);
    }

    public void addTransactionNotifyTask(PayTransactionDO transaction, PayTransactionExtensionDO extension) {
        PayNotifyTaskDO payTransactionNotifyTask = this.createBasePayNotifyTaskDO(transaction.getAppId(), transaction.getNotifyUrl())
                .setType(PayNotifyType.TRANSACTION.getValue());
        // 设置 Transaction 属性
        payTransactionNotifyTask.setTransaction(new PayNotifyTaskDO.Transaction().setOrderId(transaction.getOrderId())
                .setTransactionId(extension.getTransactionId()).setTransactionExtensionId(extension.getId()));
        payTransactionNotifyTaskMapper.insert(payTransactionNotifyTask);
        sendNotifyMessage(payTransactionNotifyTask);
    }

    public PayNotifyTaskDO createBasePayNotifyTaskDO(String appId, String notifyUrl) {
        return new PayNotifyTaskDO()
                .setAppId(appId)
                .setStatus(PayTransactionNotifyStatusEnum.WAITING.getValue())
                .setNotifyTimes(0).setMaxNotifyTimes(PayNotifyTaskDO.NOTIFY_FREQUENCY.length + 1)
                .setNextNotifyTime(DateUtil.addDate(Calendar.SECOND, PayNotifyTaskDO.NOTIFY_FREQUENCY[0]))
                .setNotifyUrl(notifyUrl);
    }

    public void sendNotifyMessage(PayNotifyTaskDO notifyTask) {

        if (PayNotifyType.TRANSACTION.getValue().equals(notifyTask.getType())) {
            defaultMQProducer.setProducerGroup("pay-provider-group-" + PayTransactionSuccessMessage.TOPIC);
            try {
                Message sendMsg = new Message(PayTransactionSuccessMessage.TOPIC,
                        "rocketTag",
                        "", JacksonUtils.toJSONString( PayNotifyConvert.INSTANCE.convertTransaction(notifyTask)).getBytes());
                defaultMQProducer.send(sendMsg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (PayNotifyType.REFUND.getValue().equals(notifyTask.getType())) {
            defaultMQProducer.setProducerGroup("pay-provider-group-" + PayRefundSuccessMessage.TOPIC);
            try {
                Message sendMsg = new Message(PayRefundSuccessMessage.TOPIC,
                        "rocketTag",
                        "", JacksonUtils.toJSONString( PayNotifyConvert.INSTANCE.convertTransaction(notifyTask)).getBytes());
                defaultMQProducer.send(sendMsg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new IllegalArgumentException(String.format("通知任务(%s) 无法发送通知消息", notifyTask.toString()));
        }
    }

}
